<template>
  <view class="container">
    <!-- 顶部区域 -->
    <!-- <view class="header"> -->
    <!-- <text class="iconfont icon-back back-icon" @click="goBack">&#xe697;</text> -->
    <!-- <text class="title">体脂率检测</text> -->
    <!-- <text class="iconfont icon-setting" @click="openSetting">&#xe6a2;</text> -->
    <!-- </view> -->

    <!-- 个人信息输入区 -->
    <view class="input-section">
      <view class="card gender-card">
        <text class="card-title">性别</text>
        <view class="gender-selector">
          <view
            :class="['gender-option', gender === 'male' ? 'active' : '']"
            @click="selectGender('male')"
          >
            <image class="gender-image" :src="maleImageUrl" mode="aspectFit" />
            <text>男性</text>
          </view>
          <view
            :class="['gender-option', gender === 'female' ? 'active' : '']"
            @click="selectGender('female')"
          >
            <image
              class="gender-image"
              :src="femaleImageUrl"
              mode="aspectFit"
            />
            <text>女性</text>
          </view>
        </view>
      </view>

      <view class="card">
        <text class="card-title">年龄</text>
        <view class="number-box">
          <button class="number-btn" @click="decreaseAge">-</button>
          <input
            type="number"
            v-model="age"
            class="number-input"
            :min="1"
            :max="120"
          />
          <button class="number-btn" @click="increaseAge">+</button>
        </view>
      </view>

      <view class="card">
        <text class="card-title">身高 (cm)</text>
        <slider
          :min="100"
          :max="220"
          :value="height"
          @changing="onHeightChanging"
          @change="onHeightChange"
          activeColor="#6C5CE7"
          backgroundColor="#E0E0E0"
          :show-value="false"
        />
        <text class="value-text">{{ height }}cm</text>
      </view>

      <view class="card">
        <text class="card-title">体重 (kg)</text>
        <slider
          :min="30"
          :max="200"
          :value="weight"
          @changing="onWeightChanging"
          @change="onWeightChange"
          activeColor="#6C5CE7"
          backgroundColor="#E0E0E0"
          :show-value="false"
        />
        <text class="value-text">{{ weight }}kg</text>
      </view>
    </view>

    <!-- 结果展示区 -->
    <view class="result-section" v-if="showResult">
      <view class="result-circle">
        <text class="result-value">{{ bfpResult }}</text>
        <text class="result-label">BMI指数</text>
      </view>
      <view class="result-info">
        <text class="result-range">正常范围: {{ getBMIRange() }}</text>
        <text class="result-status">{{ resultStatus }}</text>
        <text class="result-advice">{{ resultAdvice }}</text>
      </view>
    </view>

    <!-- 功能区 -->
    <view class="function-bar">
      <view class="function-item" @click="viewHistory">
        <text class="iconfont ls"></text>
        <text>历史记录</text>
      </view>
      <view class="function-item" @click="viewAdvice">
        <text class="iconfont jy"></text>
        <text>专业建议</text>
      </view>
      <!-- <view class="function-item" @click="shareResult">
        <text class="iconfont">&#xe6a5;</text>
        <text>分享</text>
      </view> -->
    </view>

    <!-- 测试按钮 -->
    <button class="test-button" @click="showResult ? resetTest() : startTest()">
      {{ showResult ? "重新测算" : "开始测算" }}
    </button>
  </view>
</template>

<script lang="ts" setup>
import { ref } from "vue";

const gender = ref("male");
const age = ref(25);
const height = ref(170);
const weight = ref(65);
const showResult = ref(false);
const bfpResult = ref(0);
const resultStatus = ref("");
const resultAdvice = ref("");

const maleImageUrl =
  "https://ai-public.mastergo.com/ai/img_res/86b8586673e57ac242db14f83dadef40.jpg";
const femaleImageUrl =
  "https://ai-public.mastergo.com/ai/img_res/f2aa5bcba44aa7501cf2997138259757.jpg";

const selectGender = (value: string) => {
  gender.value = value;
};

const decreaseAge = () => {
  if (age.value > 1) {
    age.value--;
  }
};

const increaseAge = () => {
  if (age.value < 120) {
    age.value++;
  }
};

const onHeightChanging = (e: any) => {
  height.value = e.detail.value;
  showTip(`身高: ${e.detail.value}cm`);
};

const onWeightChanging = (e: any) => {
  weight.value = e.detail.value;
  showTip(`体重: ${e.detail.value}kg`);
};

const onHeightChange = (e: any) => {
  height.value = e.detail.value;
};

const onWeightChange = (e: any) => {
  weight.value = e.detail.value;
};

const calculateBMI = () => {
  const bmi = Number((weight.value / (height.value / 100) ** 2).toFixed(1));
  return bmi;
};

const getBMIStatus = (bmi: number) => {
  const ageVal = Number(age.value);

  if (ageVal >= 80) {
    if (bmi < 22) return "体重偏低";
    if (bmi <= 26.9) return "体重正常";
    return "体重偏高";
  } else if (ageVal >= 65) {
    if (bmi < 20) return "体重偏低";
    if (bmi <= 26.9) return "体重正常";
    return "体重偏高";
  } else {
    if (bmi < 18.5) return "体重偏低";
    if (bmi <= 23.9) return "体重正常";
    return "体重偏高";
  }
};

const getBMIAdvice = (status: string) => {
  switch (status) {
    case "体重正常":
      return "恭喜！您的体重在健康范围内，请继续保持健康的生活方式";
    case "体重偏低":
      return "建议适当增加营养摄入，注意均衡饮食，逐步达到健康体重";
    case "体重偏高":
      return "建议控制饮食摄入，增加运动量，逐步降至健康体重";
    default:
      return "建议咨询专业医生，制定健康计划";
  }
};

const getBMIRange = () => {
  const ageVal = Number(age.value);
  if (ageVal >= 80) return "22-26.9";
  if (ageVal >= 65) return "20-26.9";
  return "18.5-23.9";
};

const startTest = () => {
  const bmi = calculateBMI();
  bfpResult.value = bmi;
  resultStatus.value = getBMIStatus(bmi);
  resultAdvice.value = getBMIAdvice(resultStatus.value);
  showResult.value = true;
};

const resetTest = () => {
  gender.value = "male";
  age.value = 25;
  height.value = 170;
  weight.value = 65;

  showResult.value = false;
  bfpResult.value = 0;
  resultStatus.value = "";
  resultAdvice.value = "";

  uni.showToast({
    title: "已重置",
    icon: "success",
    duration: 1500,
  });
};

const goBack = () => {
  uni.navigateBack();
};

const openSetting = () => {
  uni.showToast({
    title: "设置功能开发中",
    icon: "none",
  });
};

const viewHistory = () => {
  uni.showToast({
    title: "历史记录开发中",
    icon: "none",
  });
};

const viewAdvice = () => {
  uni.showToast({
    title: "专业建议开发中",
    icon: "none",
  });
};

const shareResult = () => {
  uni.showToast({
    title: "分享功能开发中",
    icon: "none",
  });
};

// 显示提示
const showTip = (message: string) => {
  uni.showToast({
    title: message,
    icon: "none",
    duration: 200,
    position: "center",
  });
};
</script>

<style>
/* 添加图标字体 */
@font-face {
  font-family: "iconfont";
  src: url("//at.alicdn.com/t/font_3869446_u4qx9vr42v.woff2") format("woff2");
}

.iconfont {
  font-family: "iconfont" !important;
  font-size: 24px;
  font-style: normal;
  color: #fff;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

page {
  height: 100%;
}

.container {
  min-height: 100%;
  background: linear-gradient(135deg, #8e2de2 0%, #4a00e0 100%);
  padding: 0 30rpx;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

.header {
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 40rpx;
  padding: 0 20rpx;
}

.title {
  width: 100%;
  text-align: center;
  color: #fff;
  font-size: 18px;
  font-weight: 600;
}

.input-section {
  flex: 1;
  overflow: auto;
  margin-top: 20rpx;
}

.card {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  backdrop-filter: blur(10px);
}

.card-title {
  font-size: 16px;
  color: #333;
  margin-bottom: 20rpx;
  display: block;
}

.gender-selector {
  display: flex;
  justify-content: space-around;
}

.gender-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20rpx;
  border-radius: 16rpx;
  transition: all 0.3s;
}

.gender-option.active {
  background: rgba(108, 92, 231, 0.1);
}

.gender-image {
  width: 120rpx;
  height: 120rpx;
  margin-bottom: 16rpx;
}

.value-text {
  margin-top: 16rpx;
  color: #666;
  font-size: 14px;
  display: block;
  text-align: center;
}

.result-section {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20rpx;
  padding: 40rpx;
  margin: 30rpx 0;
  backdrop-filter: blur(10px);
  text-align: center;
}

.result-circle {
  width: 200rpx;
  height: 200rpx;
  border-radius: 50%;
  border: 8rpx solid #6c5ce7;
  margin: 0 auto 30rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.result-value {
  font-size: 32px;
  color: #6c5ce7;
  font-weight: 600;
}

.result-label {
  font-size: 14px;
  color: #666;
  margin-top: 8rpx;
}

.result-info {
  text-align: center;
}

.result-range {
  font-size: 14px;
  color: #666;
  margin-bottom: 12rpx;
  display: block;
}

.result-status {
  font-size: 18px;
  color: #333;
  font-weight: 600;
  margin-bottom: 16rpx;
  display: block;
}

.result-advice {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  display: block;
}

.function-bar {
  display: flex;
  justify-content: space-around;
  margin: 30rpx 0;
}

.function-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.function-item .iconfont {
  margin-bottom: 8rpx;
}
.function-item .ls {
  width: 50rpx;
  height: 50rpx;
  background: url(../../static/image/lsjl.png) no-repeat center center;
  background-size: 100% 100%;
}
.function-item .jy {
  width: 50rpx;
  height: 50rpx;
  background: url(../../static/image/ysjy.png) no-repeat center center;
  background-size: 100% 100%;
}
.function-item text:last-child {
  font-size: 12px;
  color: #fff;
}

.test-button {
  background: linear-gradient(135deg, #6c5ce7 0%, #4a00e0 100%);
  color: #fff;
  font-size: 16px;
  border-radius: 50rpx;
  margin: 0 0 40rpx 0;
  padding: 24rpx 0;
  transition: all 0.3s ease;
}

.test-button:active {
  transform: scale(0.98);
  opacity: 0.9;
}

.number-box {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.number-btn {
  width: 45px;
  height: 35px;
  padding: 0;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #6c5ce7;
  background: rgba(108, 92, 231, 0.1);
  border: none;
  border-radius: 8px;
}

.number-input {
  width: 70px;
  height: 35px;
  text-align: center;
  font-size: 16px;
  color: #333;
  background: rgba(108, 92, 231, 0.05);
  border: 1px solid rgba(108, 92, 231, 0.2);
  border-radius: 8px;
}

/* 优化滑块样式 */
slider {
  margin: 15px 0;
}
</style>

